//
//  SSPhoneLoginViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/8.
//

import UIKit
import Alamofire
import SwiftyJSON


class SSPhoneLoginViewController: UIViewController {
    ///手机号输入框
    @IBOutlet weak var phoneNumTextField: UITextField!
    ///验证码按钮
    @IBOutlet weak var codeButton: UIButton!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(loginView)
    }
    
    //MARK: -lazy
    // 登录视图
    lazy var loginView: LoginView = {
//        let view = LoginView.init(loginViewType.msgCode, frame: self.view.bounds)
        let view = LoginView.init(loginViewType.msgCode, frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight))
//        view.backgroundColor = UIColor.blue
        view.actionClosure = {
            [weak self](_ type: Int,_ params: LoginRequest) -> () in
            self?.network(params: params)
        }
        view.backgroundColor = UIColor.white
        return view
    }()
    
    //MARK: -network
    func network(params:LoginRequest) {
        let param = [
            "phoneNo": params.phoneNum,
            "verifyCode":params.msgCodeNum
                ] as [String:AnyObject]
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.login_URL, parameters: param) { (result, error) in
            SSProgressHUD.share.dismiss()
            if let _ = result{
                let json = JSON(parseJSON: result!)
                let token = json["data"]["token"].stringValue
                let retCode = json["retCode"].stringValue
                let retMsg = json["retMsg"].stringValue
                if(retCode == "0000"){
                    self.dealToken(token,phone: params.phoneNum)
                    
                    let vc = SSMainTabBarController()
                    let appdelegate = UIApplication.shared.delegate as! AppDelegate
                    let window = appdelegate.window
                    window?.rootViewController = vc
                    
                    window?.backgroundColor = UIColor.white
                    window?.makeKeyAndVisible()

                    
                }else{
                    dissmissToTast(msg: "\(retMsg)")
                }
            }else{
//                if let message = error?.userInfo["errorinfo"]{
//                    self.view.makeToast(message as? String)
//                }
            }
        }
    }
    
    //存储token
    func dealToken(_ token: String,phone:String) {
        kUserDefaults.setValue(phone, forKey: kUserPhoneKey)
        kUserDefaults.setValue(token, forKey: kUserTokenKey)
        kUserDefaults.synchronize()
        kNotifiDefault.post(name: .AppLogIn, object: nil)
    }
    
}





 func ex_rootWindow() -> UIWindow? {
       var rootWindow: UIWindow? = nil
       if #available(iOS 13, *) {
           rootWindow = UIApplication.shared.connectedScenes
               .compactMap { $0 as? UIWindowScene }
               .flatMap { $0.windows }
               .first
       }
       if let _ = rootWindow {}
       else if let w = UIApplication.shared.delegate?.window {
           rootWindow = w
       }
       else if let w = UIApplication.shared.windows.first {
           rootWindow = w
       }
       return rootWindow
   }


